import axios from 'axios'
import {store} from '../store'
import {notification} from 'antd'
//实例化axios
var service = axios.create({
    timeout:10*1000, //超时时间
    baseURL:'/' //基准路径( 会自动拼接在所有ajax请求地址的前面 )
})


//设置拦截器
service.interceptors.request.use(
    (config)=>{
        //设置请求头
        config.headers['token'] = store.getState().loginData.token;
        return config;
    },
    (error)=>{
        return Promise.reject(error);
    }
)
//设置拦截器
service.interceptors.response.use(
    (res)=>{
        return res;
    },
    (error)=>{
        var status = error.response.status;
        if( status == 401 ){
            notification.error({message:'登陆过期,请重新登录',duration:2});
        }
        else if( status == 404 ){
            notification.error({message:'访问路径有误',duration:2});
        }
        else if( status == 500 ){
            notification.error({message:'服务器内部错误',duration:2});
        }
        else if( status == 503 ){
            notification.error({message:'服务器不可用',duration:2});
        }
        return Promise.reject(error);
    }
)


export default service;

